﻿using System;

namespace NFox.Pycad.Core.Modules
{

    /// <summary>
    /// Pycad引擎
    /// </summary>
    public class pye
    {

        private static dynamic GetCommand(string name)
        {
            if (Application.InitCommands.ContainsKey(name))
                return Application.InitCommands[name];
            return Application.Commands[name];
        }

        private static dynamic GetLispFunction(string name)
        {
            if (Application.InitLispFunctions.ContainsKey(name))
                return Application.InitLispFunctions[name];
            return Application.LispFunctions[name];
        }

        internal static void addcommand(dynamic cmd)
        {
            if (Application.Initialized)
                Application.InitCommands.Add(cmd.Name, cmd);
            else
                Application.Commands.Add(cmd.Name, cmd);
        }

        internal static void addlisp(dynamic lisp)
        {
            if (Application.Initialized)
                Application.InitLispFunctions.Add(lisp.Name, lisp);
            else
                Application.LispFunctions.Add(lisp.Name, lisp);
        }

        public static void runsyscmd(string name)
        {
            DynamicCompiler.SystemCommands[name]();
        }

        public static void runcmd(string name)
        {
            GetCommand(name).__call__();
        }

        public static object invokelisp(string name, dynamic args)
        {
            return GetLispFunction(name).__call__(args);
        }

        private static dynamic _tempvalue;

        public static void settempvalue(dynamic value)
        {
            _tempvalue = value;
        }

        public static dynamic gettempvalue()
        {
            return _tempvalue;
        }

        public static dynamic execute(string code)
        {
            return Application.Engine.Execute(code);
        }

        public static string findfile(string modulename, string filename)
        {
            if (modulename.Contains("."))
                modulename = modulename.Substring(0, modulename.IndexOf('.'));
            return Application.Engine.Projects.GetProject(modulename).FindFile(filename);
        }

        public static string location
        {
            get { return Engine.MainPath.GetFile("NFox.Pycad.dll").FullName; }
        }

        public static string libpath
        {
            get { return Engine.Path.FullName; }
        }

        public static string mainpath
        {
            get { return Engine.MainPath.FullName; }
        }

        public static string supportpath
        {
            get { return Engine.SupportPath.FullName; }
        }

        public static void loadcuix(string name, string path)
        {
            loadcuix(name, name, path);
        }

        public static void loadcuix(string name, string modulename, string path)
        {
            if (!Application.CuixFlies.ContainsKey(name))
                Application.CuixFlies.Add(name, findfile(modulename, path));
        }

        public static Version version
        {
            get { return Application.Version; }
        }

        public static bool loadfirst
        {
            get { return Application.LoadFirst; }
        }

        public static bool shp()
        {
            return Application.Engine.ShowSysProjects();
        }

        public static void build(dynamic module)
        {
            Application.Engine.Release(
                module.__name__,
                Application.Engine.Projects.GetProject(module.__name__));
        }

    }


}


